﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Model;
using System.Data;

namespace PMS.Web
{
    public partial class Renewal : ExportFiles
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string role = PageComm.GetUser(this.Response).role;
                if (!PageComm.IsEdit(ActionMenu.Renewal, role))
                {
                    btn_Create.Visible = false;
                    btn_Add.Visible = false;
                    gv_PMS_ProjectRenewal.Columns[gv_PMS_ProjectRenewal.Columns.Count - 3].Visible = false;
                    gv_PMS_ProjectRenewal.Columns[gv_PMS_ProjectRenewal.Columns.Count - 2].Visible = false;
                }
                else
                {
                    gv_PMS_ProjectRenewal.Columns[gv_PMS_ProjectRenewal.Columns.Count - 1].Visible = false;
                }
                string projectID = Request["projectID"];
                if (!string.IsNullOrEmpty(projectID))
                {
                    hid_ProjectID.Value = projectID;
                    ProjectLoad();
                    DataLoad();
                }
            }
        }


        /// <summary>
        /// 加载项目信息
        /// </summary>
        private void ProjectLoad()
        {
            string projectID = hid_ProjectID.Value;
            PMS_ProjectBLL pbll = new PMS_ProjectBLL();
            PMS_Project project = pbll.GetModel(Convert.ToInt32(projectID));
            lab_ProjectID.Text = project.projectID;
            lab_JDEID.Text = project.JDEID;
            lab_Customer.Text = project.customer;
            lab_CustomerTel.Text = project.customerTel;
            lab_ProjectName.Text = project.projectName;
            lab_ProjectType.Text = project.projectType;
            lab_SystemPath.Text = project.systemPath;
            lab_City.Text = project.city;
        }

        /// <summary>
        /// 获取查询条件
        /// </summary>
        /// <returns></returns>
        private PMS_ProjectRenewalQuery GetQuery()
        {
            PMS_ProjectRenewalQuery query = new PMS_ProjectRenewalQuery();
            query.projectID = Convert.ToInt32(hid_ProjectID.Value);
            if (!string.IsNullOrEmpty(txt_FromDate.Text.Trim()))
                query.startDate = Convert.ToDateTime(txt_FromDate.Text.Trim());
            if (!string.IsNullOrEmpty(txt_ToDate.Text.Trim()))
                query.endDate = Convert.ToDateTime(txt_ToDate.Text.Trim());
            return query;
        }

        /// <summary>
        /// 列表绑定
        /// </summary>
        /// <param name="projectID"></param>
        private void DataLoad()
        {
            try
            {
                PMS_ProjectRenewalQuery query = GetQuery();
                PMS_ProjectRenewalBLL bll = new PMS_ProjectRenewalBLL();
                List<PMS_ProjectRenewal> liste = bll.GetList(query);
                this.gv_PMS_ProjectRenewal.DataSource = liste;
                this.gv_PMS_ProjectRenewal.DataBind();
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Create_Click(object sender, EventArgs e)
        {
            PMS_ProjectRenewalBLL bll = new PMS_ProjectRenewalBLL();
            PMS_ProjectRenewal model = new PMS_ProjectRenewal();
            model.id = Convert.ToInt32(hid_ID.Value);
            model.projectID = hid_ProjectID.Value;
            model.bugDescription = txt_BugDescription.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_RepairsDate.Text))
                {
                    model.repairsDate = Convert.ToDateTime(txt_RepairsDate.Text);
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('报修日期格式错误');</script>");
                return;
            }

            model.jobID = txt_ID.Text.Trim();
            try
            {
                if (!string.IsNullOrEmpty(txt_MaintainDate.Text.Trim()))
                {
                    model.maintainDate = Convert.ToDateTime(txt_MaintainDate.Text.Trim());
                }
            }
            catch
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('处理日期格式错误');</script>");
                return;
            }

            model.maintainDescription = txt_MaintainDescription.Text.Trim();
            model.repairsTel = txt_RepairsTel.Text.Trim();
            model.bugType = ddl_BugType.Text;
            model.device = txt_Device.Text;
            model.deviceType = txt_DeviceType.Text;
            model.maintainPeople = txt_MaintainPeople.Text;
            model.repairsPeople = txt_RepairsName.Text;
            model.status = ddl_State.Text;
            if (model.status == "已解决")
            {
                model.completeDate = DateTime.Now.Date;
            }

            if (!bll.AddUpdate(model))
            {
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('更新失败，请稍后重试');</script>");
                return;
            }
            else
            {
                ClearText();
                DataLoad();
                this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(0);</script>");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        public void ClearText()
        {
            hid_ID.Value = "0";
            txt_BugDescription.Text = "";
            txt_RepairsDate.Text = "";
            txt_ID.Text = "";
            txt_MaintainDate.Text = "";
            txt_MaintainDescription.Text = "";
            txt_RepairsTel.Text = "";
        }

        /// <summary>
        /// 加载当前记录
        /// </summary>
        /// <param name="id"></param>
        public void LoadEditText(int id)
        {
            PMS_ProjectRenewalBLL bll = new PMS_ProjectRenewalBLL();
            PMS_ProjectRenewal model = bll.GetModel(id);
            hid_ID.Value = model.id.ToString();
            txt_BugDescription.Text = model.bugDescription;
            if (model.repairsDate != null)
            {
                txt_RepairsDate.Text = model.repairsDate.Value.ToString("yyyy-MM-dd");
            }
            txt_ID.Text = model.jobID;
            if (model.maintainDate != null)
            {
                txt_MaintainDate.Text = model.maintainDate.Value.ToString("yyyy-MM-dd");
            }
            txt_MaintainDescription.Text = model.maintainDescription;
            txt_RepairsTel.Text = model.repairsTel;
            if (ddl_BugType.Items.FindByText(model.bugType) != null)
            {
                ddl_BugType.Items.FindByText(model.bugType).Selected = true;
            }

            txt_Device.Text = model.device;
            txt_DeviceType.Text = model.deviceType;
            txt_MaintainPeople.Text = model.maintainPeople;
            txt_RepairsName.Text = model.repairsPeople;

            if (ddl_State.Items.FindByText(model.status) != null)
            {
                ddl_State.Items.FindByText(model.status).Selected = true;
            }

            if (model.status == "已解决")
            {
                lab_CompleteTitle.Text = "完成日期";
                lab_CompleteDate.Text = model.completeDate.Value.ToString("yyyy-MM-dd");
            }
            else
            {
                lab_CompleteTitle.Text = "预计日期";
                lab_CompleteDate.Text = DateTime.Now.AddDays(1).ToString("yyyy-MM-dd");
            }

            this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>showContentDiv(1);</script>");
        }

        protected void gv_PMS_ProjectRenewal_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Edited" || e.CommandName == "Details")
            {
                if (e.CommandArgument != null)
                {
                    LoadEditText(Convert.ToInt32(e.CommandArgument));
                }
            }
            else if (e.CommandName == "Del")
            {
                if (e.CommandArgument != null)
                {
                    PMS_ProjectRenewalBLL bll = new PMS_ProjectRenewalBLL();
                    if (bll.Delete(Convert.ToInt32(e.CommandArgument)))
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除成功');</script>");
                        DataBind();
                    }
                    else
                    {
                        this.ClientScript.RegisterStartupScript(GetType(), "msg", "<script>alert('删除失败');</script>");
                    }
                }
            }
        }

        protected void gv_PMS_ProjectRenewal_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            GridViewRow row = e.Row;
            if (row.RowType == DataControlRowType.DataRow)
            {
                PMS_ProjectRenewal model = row.DataItem as PMS_ProjectRenewal;
                string repairsDate = "";
                string completeDate = "";
                if (model.repairsDate != null && model.repairsDate != DateTime.MinValue)
                {
                    repairsDate = model.repairsDate.Value.ToString("yyyy-MM-dd");
                }
                if (model.completeDate != null && model.completeDate != DateTime.MinValue)
                {
                    completeDate = model.completeDate.Value.ToString("yyyy-MM-dd");
                }
                row.Cells[1].Text = repairsDate;
                row.Cells[6].Text = completeDate;
            }
        }

        protected void btn_Search_Click(object sender, EventArgs e)
        {
            DataLoad();
        }

        protected void btn_Export_Click(object sender, EventArgs e)
        {
            PMS_ProjectRenewalQuery query = GetQuery();
            PMS_ProjectRenewalBLL bll = new PMS_ProjectRenewalBLL();
            DataTable dt = bll.GetListTable(query);
            if (dt != null && dt.Rows.Count > 0)
            {
                ExportExcel("维修项目清单", dt);
            }
        }
    }
}